<?php

require_once 'src/model/mVenda.php';

class aVenda extends mVenda {
    /* datefomat ver data alua 06 
     * mysql : date_format(data_cad, '%s') as data_cad
     */
    protected $sqlInsert = "INSERT INTO venda( data, hora, total, idfuncionario, idpet, idcliente) VALUES ('%s','%s','%s','%s','%s', '%s' )";
    protected $sqlUpdate = "UPDATE venda SET data='%s',hora='%s',total='%s',idfuncionario='%s',idpet='%s',idcliente='%s' WHERE idvenda = '%s'";
    protected $sqlSelect = "SELECT * FROM venda WHERE 1=1 %s %s";
    protected $sqlSelectNomesAbertas = "SELECT v.idvenda, v.data, v.hora, v.total, f.nome as funcionario, p.nome as pet, c.nome as cliente
                            FROM venda v
                            JOIN funcionario f USING(idfuncionario)
                            JOIN pet p USING(idpet)
                            JOIN cliente c ON c.idcliente = v.idcliente
                            WHERE v.total = 0 ORDER BY data desc, hora desc";
    
    protected $sqlSelectNomesFechadas = "SELECT v.idvenda, v.data, v.hora, v.total, f.nome as funcionario, p.nome as pet, c.nome as cliente
                            FROM venda v
                            JOIN funcionario f USING(idfuncionario)
                            JOIN pet p USING(idpet)
                            JOIN cliente c ON c.idcliente = v.idcliente
                            WHERE v.total > 0 
                            ORDER BY data desc , hora desc";
    protected $sqlDelete = "DELETE FROM venda WHERE idvenda = '%s'";

    public function insert() {
        $sql = sprintf($this->sqlInsert, $this->getData(), $this->getHora(), 
                $this->getTotal(), $this->getIdfuncionario(), $this->getIdpet(), 
                $this->getIdcliente());
        return $this->RunQuery($sql);
    }

    public function update() {
        $sql = sprintf($this->sqlUpdate, $this->getData(), $this->getHora(), 
                $this->getTotal(), $this->getIdfuncionario(), $this->getIdpet(), 
                $this->getIdcliente(), $this->getIdvenda());
        return $this->RunQuery($sql) or die ($sql);
    }

    public function delete() {
        $sql = sprintf($this->sqlDelete, $this->getIdvenda());
        return $this->RunQuery($sql);
    }
    
    public function select($where='', $order='') {
        $sql = sprintf($this->sqlSelect, $where, $order);
        return $this->RunSelect($sql);
    }
    
    public function selectNomesAbertas($where='', $order='') {
        $sql = sprintf($this->sqlSelectNomesAbertas, $where, $order);
        return $this->RunSelect($sql);
    }
    
    public function selectNomesFechadas($where='', $order='') {
        $sql = sprintf($this->sqlSelectNomesFechadas, $where, $order);
        return $this->RunSelect($sql);
    }
    
    public function load() {
        $rs = $this->select(sprintf("and idvenda='%s'", $this->getIdvenda()));
        $this->setObjVenda($rs[0]['idvenda'], $rs[0]['data'], $rs[0]['hora'], $rs[0]['total'],
                $rs[0]['idfuncionario'], $rs[0]['idpet'], $rs[0]['idcliente']);
        return $this;
    }
    
}
